﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Data.Abstract;
using Proggy.Data.Entities;

namespace Proggy.Data.Concrete
{
    /// <summary>
    /// The random data repository
    /// </summary>
    public class RandomDataRepository : IRandomDataRepository
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public RandomDataRepository()
        {
            this._dc = new ProggyDataContext();
        }

        /// <summary>
        /// Data context
        /// </summary>
        private ProggyDataContext _dc;

        /// <summary>
        /// Data items
        /// </summary>
        public IQueryable<RandomDataItem> RandomDataItems
        {
            get
            {
                return this._dc.RandomDataItems;
            }
        }

        /// <summary>
        /// Create new
        /// </summary>
        /// <param name="id"></param>
        /// <param name="category"></param>
        /// <param name="value"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool GetCreateOk(string id, string category, string value, string currentUsername, ref string errorMsg)
        {
            try
            {
                //
                var now = DateTime.Now;

                // new
                var newItem = new RandomDataItem
                {
                    RandomDataItemId = id,
                    CategoryAlias = category,
                    Value = value,
                    FirstCreated = now,
                    LastUpdated = now,
                    FirstCreatedUsername = currentUsername,
                    LastUpdatedUsername = currentUsername
                };

                //
                this._dc.RandomDataItems.Add(newItem);

                //
                this._dc.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : "");
                errorMsg = "An error has occured. Make sure the \"unique name\" is actually unique and that all other fields are correctly filled in.";
                return false;
            }
        }

        /// <summary>
        /// Edit existing
        /// </summary>
        /// <param name="id"></param>
        /// <param name="category"></param>
        /// <param name="value"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool GetEditOk(string id, string category, string value, string currentUsername, ref string errorMsg)
        {
            try
            {
                // find 
                var item = this.RandomDataItems.SingleOrDefault(x => x.RandomDataItemId == id);

                // check
                if (item == null)
                {
                    errorMsg = string.Format("Item {0} does not exist!", id);
                    return false;
                }

                // update
                item.RandomDataItemId = id;
                item.CategoryAlias = category;
                item.Value = value;
                item.LastUpdated = DateTime.Now;
                item.LastUpdatedUsername = currentUsername;


                //
                this._dc.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : "");
                errorMsg = "An error has occured. Make sure the \"unique \name is actually unique and that all other fields are correctly filled in.";
                return false;
            }
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="dataId"></param>
        public void Delete(string dataId)
        {
            // find 
            var item = this.RandomDataItems.SingleOrDefault(x => x.RandomDataItemId == dataId);

            // check
            if (item != null)
            {
                this._dc.RandomDataItems.Remove(item);
                this._dc.SaveChanges();
            }
        }

        /// <summary>
        /// Get data by category, ordered by value
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public IQueryable<RandomDataItem> GetDataByCategoryName(string category)
        {
            return this.RandomDataItems.Where(x => x.CategoryAlias == category).OrderBy(x => x.Value).AsQueryable();
        }
    }
}
